module params
implicit none

!1 = no CC
!2 = CC with commitment to r_b and bbar (can increase bbar)
!3 = CC with commitment to bbar
!4 = CC with commitment to r_b
!5 = CC without commitment

double precision, parameter :: zero=0, one=1
double precision, parameter :: r=.0344, delta_k=.045, tau_c=0.034    !interest rate, depreciation rate, transaction cost
double precision, parameter :: zeta=.37
double precision, parameter :: alpha=.36, sigma=2.  !capital share, risk aversion
double precision, parameter :: gamma_e=0.57721  !Euler's constant for type 1 shocks
double precision, parameter :: phi2=0
integer, parameter :: compute_regression=1
double precision :: phi1, phi1_end, phi3        !phi1 is ATP issuance, phi3 is ATP limit increases
double precision :: theta, k, w, y  !labor productivity, aggregate capital, wage rate, and output

double precision, parameter :: penalty=1e38         !penalty for negative consumption
double precision, parameter :: tol_brent=1e-11
integer, parameter :: otcs=1
integer :: save_vfn
integer, parameter :: contract2=1, contract3=0, contract4=0, contract5=0
double precision :: tau_qcap

!parameters to be calibrated
double precision :: delta    !discount factor
double precision :: gamma   !scaling parameter for type 1 shocks
double precision :: kappa   !cost of credit offer   
double precision :: A       !matching efficiency
double precision :: pen_f   !default utility cost
double precision :: alpha_u !consumer share in matching function
double precision :: rr      !social security replacement rate
double precision :: quasi_hyperbolic_share

! !earnings process
! double precision, parameter :: mu1=.119, sigma1=.325, mu2=-.026, sigma2=.001,p1=.490, p2=one-p1
! double precision, parameter :: mu_eta=mu1*p1+mu2*p2, rho_eta=.979, var_eta=(sigma1*p1+sigma2*p2)**2.
! integer, parameter :: neta=31
! double precision, parameter :: mu_z=0, rho_z=0, var_z=.186**2  !transitory
double precision, parameter :: mu_eta=0 !persistent
integer, parameter :: neta=13
double precision, parameter :: mu_z=0, rho_z=0  !transitory
integer, parameter :: nz=1
double precision, parameter :: mu_nu=0, rho_nu=0   !permanent
integer, parameter :: nnu=3

integer, parameter :: nu1=7
double precision :: grid_u1(nu1), pi_u1(nu1)
integer :: iu1, ju1
double precision :: var_z, var_eta, var_nu, rho_eta

!expense shock grid size
integer, parameter :: nx=1

!debt, productivity, borrowing limit, borrowing rate, number of beta-types
integer, parameter :: nj=60, nb=11, neps=neta*nz*nnu*nx, nbbar=3, nrb=3, nq=nrb, nbeta=2
integer, parameter :: jr=46 !retired at 65
integer, parameter :: intbirth_b=6 !integer at which b=0

!age-specific parameters
double precision, dimension(nj) :: psi, nu_j
double precision :: SS(nnu), SS_total, tau

!counters
integer(kind=2) :: ib, jb
integer :: ij, ieps, ibbar, irb, iq, ibeta
integer :: icontract, jcontract
integer :: jeps, jbbar, jrb
integer :: id_nu(neps), inu
integer :: ix, jx
integer, dimension(neps) :: ix_ieps
integer :: iz, ieta

!grid
double precision :: grid_b(nnu,nbbar,nb), grid_x(nx), grid_eps(neps), grid_bbar(nnu,nbbar), grid_tau_q(nq), grid_beta(nbeta)
integer :: id_bbar(nnu,nbbar)

double precision, dimension(neps) :: put_eps
double precision, dimension(neps,neps) :: pi_eps
double precision, dimension(nbeta) :: put_beta

!bounds for b and bbar
double precision, parameter :: bmax=25
double precision, parameter :: bbarmax=zero

!policy functions, value function, distribution, and bond price function
integer, allocatable, dimension(:,:,:,:,:) :: g_b1_int
double precision, allocatable, dimension(:,:,:,:,:) :: g_df1
integer, allocatable, dimension(:,:,:,:,:,:,:) :: g_b2_int
double precision, allocatable, dimension(:,:,:,:,:,:,:) :: g_df2
integer, allocatable, dimension(:,:,:,:,:,:) :: g_b3_int
double precision, allocatable, dimension(:,:,:,:,:,:) :: g_df3
integer, allocatable, dimension(:,:,:,:,:,:) :: g_b4_int
double precision, allocatable, dimension(:,:,:,:,:,:) :: g_df4
integer, allocatable, dimension(:,:,:,:,:) :: g_b5_int
double precision, allocatable, dimension(:,:,:,:,:) :: g_df5

!mpi variables
integer :: ierr, rank, nproc, iproc, cnt
integer, parameter :: ni=nb*neps*nbbar*nrb*nbeta
integer, parameter :: ni_bargain=(nb-intbirth_b+1)*neps*nbeta
integer :: itop, iend, np
integer :: itop_bargain, iend_bargain, np_bargain
integer(kind=2) :: zmap(ni,5), zmap_bargain(ni_bargain,3)
integer :: zmap_inv(nb,neps,nbbar,nrb,nbeta), zmap_bargain_inv(nb,neps,nbeta)

integer, parameter :: ni3=nb*neps*nbbar*nbeta
integer :: itop3, iend3, np3
integer(kind=2) :: zmap3(ni3,4)
integer :: zmap3_inv(nb,neps,nbbar,nbeta)

integer, parameter :: ni4=nb*neps*nrb*nbeta
integer :: itop4, iend4, np4
integer(kind=2) :: zmap4(ni4,4)
integer :: zmap4_inv(nb,neps,nrb,nbeta)

integer, parameter :: ni5=nb*neps*nbeta
integer :: itop5, iend5, np5
integer(kind=2) :: zmap5(ni5,3)
integer :: zmap5_inv(nb,neps,nbeta)

!global variables for optimization
double precision :: b, eps, bbar, q, c, bp, xp, df_prob, rb, prob_temp, beta
integer(kind=1) :: df
double precision, dimension(nj) :: q_save

!global variables for aggregates
double precision :: lab, con, excess, exdem, var_con

!probability of credit access and solution for CC limits and CC interest rate
double precision, allocatable, dimension(:,:,:,:) :: prob
integer(kind=2), allocatable, dimension(:,:,:,:) :: g_bbar, g_rb
integer(kind=1), allocatable, dimension(:,:,:,:) :: g_contract

double precision, allocatable, dimension(:,:,:,:,:,:) :: prob2
integer(kind=2), allocatable, dimension(:,:,:,:,:,:) :: g_bbar2, g_rb2
integer(kind=1), allocatable, dimension(:,:,:,:,:,:) :: g_contract2
integer(kind=2), allocatable, dimension(:,:,:,:,:,:) :: g_limit2

double precision, allocatable, dimension(:,:,:,:,:) :: prob3
integer(kind=2), allocatable, dimension(:,:,:,:,:) :: g_bbar3, g_rb3
integer(kind=1), allocatable, dimension(:,:,:,:,:) :: g_contract3
integer(kind=2), allocatable, dimension(:,:,:,:,:) :: g_q3
integer(kind=2), allocatable, dimension(:,:,:,:,:) :: g_limit3

double precision, allocatable, dimension(:,:,:,:,:) :: prob4
integer(kind=2), allocatable, dimension(:,:,:,:,:) :: g_bbar4, g_rb4
integer(kind=1), allocatable, dimension(:,:,:,:,:) :: g_contract4
integer(kind=2), allocatable, dimension(:,:,:,:,:) :: g_limit4

double precision, allocatable, dimension(:,:,:,:) :: prob5
integer(kind=2), allocatable, dimension(:,:,:,:) :: g_bbar5, g_rb5
integer(kind=1), allocatable, dimension(:,:,:,:) :: g_contract5
integer(kind=2), allocatable, dimension(:,:,:,:) :: g_q5
integer(kind=2), allocatable, dimension(:,:,:,:) :: g_limit5

!unsecured credit market statistics
double precision :: acc, credit, def_rate, pop_debt, avg_spread, std_spread, charge_off, avg_bbar, default_with_assets, repayers_defaulting, &
access_young, limit_increases, avg_bbar_all, limit_decreases, limit_max
double precision, dimension(5) :: acc_i, debtor_i, limit_i, rb_i, income_i, debt_i, limit_all_i, limit_max_i
double precision, dimension(nnu) :: acc_type, credit_type, def_rate_type, pop_debt_type, rb_type, charge_off_type, pop_type, limit_type, limit_all_type, &
limit_max_type

double precision :: contracts(5), contracts_i(5,5), contracts_type(nnu,5)
double precision :: contracts_debt_i(5,5)

double precision :: disp_inc, disp_inc_pc, disp_inc_age(nj)
double precision :: avg_eps

double precision :: disposable_earnings

double precision, dimension(nj) :: pop_by_age, access_by_age, def_rate_by_age, avg_spread_by_age, pop_debt_by_age,&
avg_bbar_by_age, con_by_age, earnings_by_age, assets_by_age, avg_bbar_all_by_age, limit_max_age, var_con_by_age                 
double precision, dimension(nj+1) :: credit_by_age, charge_off_by_age

!variables for welfare
character :: stst
double precision, allocatable, dimension(:,:,:,:) :: v1_beg, v1_end
double precision, allocatable, dimension(:,:,:,:,:,:) :: v2_beg, v2_end
double precision :: vote_unborn, vote_all, vote_income(5), vote_age(nj)
double precision :: wev_unborn, wev_all, wev_income(5), wev_age(nj)
double precision :: wev_all_beta(nbeta), wev_income_beta(5,nbeta), wev_age_beta(nj,nbeta)
double precision :: wcv_unborn, wcv_all, wcv_income(5), wcv_age(nj)
double precision :: v_beg, v_end, v_final, vtemp
double precision :: wev_winner, wev_loser, wcv_winner, wcv_loser
double precision :: wev_age_income(nj,5)

double precision :: prob_end_unborn(nb,neps,nbeta)
integer(kind=2) :: g_bbar_end_unborn(nb,neps,nbeta), g_rb_end_unborn(nb,neps,nbeta)
integer(kind=1) :: g_contract_end_unborn(nb,neps,nbeta)
double precision, parameter :: tau_qmin=(1+r)/(1-tau_c*(1+r))-1-r
double precision :: tau_qmax


!transition variables
integer, parameter :: compute_transition=0, t1=14 !t=1 (2008), t=2(2009), ...., t=12 (2019)
double precision, dimension(t1+1) :: con_t, credit_t, def_rate_t, pop_debt_t, avg_spread_t, charge_off_t, avg_bbar_t, avg_bbar_all_t, acc_t, var_con_t&
, wev_all_t
double precision, dimension(t1+1,nnu) :: acc_type_t, credit_type_t, def_rate_type_t, pop_debt_type_t, rb_type_t, charge_off_type_t, &
limit_type_t, limit_all_type_t    
double precision, dimension(t1+1,nj) :: con_by_age_t, assets_by_age_t, earnings_by_age_t, def_rate_by_age_t, &
avg_spread_by_age_t, pop_debt_by_age_t, avg_bbar_by_age_t, avg_bbar_all_by_age_t, access_by_age_t, var_con_by_age_t
double precision, dimension(t1+1,nj+1) :: credit_by_age_t, charge_off_by_age_t
double precision, dimension(t1+1) :: default_with_assets_t, repayers_defaulting_t, limit_increases_t, limit_decreases_t

double precision, dimension(t1+1) :: disp_inc_t, disp_inc_pc_t

double precision :: balance_transfers, balance_transfer_debt
double precision, dimension(neta) :: put_eta

double precision :: utilization_above_10, utilization_above_25, utilization_above_50, utilization_above_75
double precision, dimension(5) :: utilization_above_10_i, utilization_above_25_i, utilization_above_50_i, utilization_above_75_i

!regression variables
integer, parameter :: reg_k=7
double precision :: reg_beta(reg_k)
integer :: reg_n, reg_n_beg, reg_n_end

double precision :: delta_old, delta_new
double precision :: pen_f_old, pen_f_new

double precision, dimension(nnu*nbbar*intbirth_b) :: grid_utilization
integer :: cnt_utilization

double precision :: avg_utilization, med_utilization, avg_utilization_all, avg_utilization_all_i(5), full_utilization
double precision :: avg_utilization_i(5), med_utilization_i(5)
integer :: grid_utilization_inv(nnu,nbbar,intbirth_b)
double precision :: std_log_earnings_change_1, std_log_earnings_change_5, std_log_earnings_perm, correlation_lag_1, log_earnings_p_90_10

double precision :: correlation_lag_t(5)

double precision, parameter :: std_log_earnings_change_1_2004=0.5784, std_log_earnings_change_5_2004=0.8283, std_log_earnings_perm_2004=0.7818, &
correlation_lag_1_2004=0.7619, log_earnings_p_90_10_2004=1.919, correlation_lag_5_2004=0.599, correlation_lag_2_2004=0.7, correlation_lag_3_2004=.6628, &
correlation_lag_4_2004=.6297

double precision, parameter :: std_log_earnings_change_1_2019=0.5399, std_log_earnings_change_5_2019=0.7636, std_log_earnings_perm_2019=0.7986, &
correlation_lag_1_2019=0.7882, log_earnings_p_90_10_2019=1.961, correlation_lag_5_2019=0.6387, correlation_lag_2_2019=0.7277, correlation_lag_3_2019=.6919, correlation_lag_4_2019=.6645

integer :: iter_calibrate, iter
double precision, allocatable, dimension(:,:,:,:) :: rn 

end module params     
        
